package com.leo.chapter08;

import java.util.concurrent.*;

/**
 *
 * @author xuexiaolei
 * @version 2017年10月16日
 */
public class Test {
    ///创建了一个固定大小的线程池，采用有界队列及“调用者运行”饱和策略
    private final int N_THREADS = 10;
    private final int CAPACITY = 50;
    private final ThreadPoolExecutor executor =
        new ThreadPoolExecutor(N_THREADS, N_THREADS, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(CAPACITY), new ThreadFactory() {
            @Override public Thread newThread(Runnable r) {
                return new Thread(r);
            }
        }, new ThreadPoolExecutor.CallerRunsPolicy());

    /**
     * 对通过工厂方法创建的线程池进行修改
     */
    public void changeDefaultExector(){
        ExecutorService exec = Executors.newCachedThreadPool();
        if (exec instanceof ThreadPoolExecutor){
            ((ThreadPoolExecutor) exec).setCorePoolSize(10);
        }else{
            throw new AssertionError("Oops, bad assumption");
        }
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        exec.submit(new Runnable() {
            @Override public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("hello");
            }
        });
        exec.shutdown();
    }
}
